// ignore_for_file: constant_identifier_names
import 'package:flutter/material.dart';

part 'date_format.dart';

class DateUtil {
  static const num ONE_MINUTE = 60000;
  static const num ONE_HOUR = 3600000;
  static const num ONE_DAY = 86400000;
  static const num ONE_WEEK = 604800000;

  static const String ONE_SECOND_AGO = "秒前";
  static const String ONE_MINUTE_AGO = "分钟前";
  static const String ONE_HOUR_AGO = "小时前";
  static const String ONE_DAY_AGO = "天前";
  static const String ONE_MONTH_AGO = "月前";
  static const String ONE_YEAR_AGO = "年前";

//时间转换
  static String format(DateTime date) {
    num delta =
        DateTime.now().millisecondsSinceEpoch - date.millisecondsSinceEpoch;
    if (delta < 1 * ONE_MINUTE) {
      num seconds = _toSeconds(delta);
      return (seconds <= 0 ? 1 : seconds).toInt().toString() + ONE_SECOND_AGO;
    }
    if (delta < ONE_HOUR) {
      num minutes = _toMinutes(delta);
      return (minutes <= 0 ? 1 : minutes).toInt().toString() + ONE_MINUTE_AGO;
    }
    if (delta < 24 * ONE_HOUR) {
      num hours = _toHours(delta);
      return (hours <= 0 ? 1 : hours).toInt().toString() + ONE_HOUR_AGO;
    }
    if (delta < 48 * ONE_HOUR) {
      return "昨天";
    }
    if (delta < 3 * 24 * ONE_HOUR) {
      return "2" + ONE_DAY_AGO;
    }

    return DateFormat.format(date, DateFormatType.ymd);
  }

  static int _toSeconds(num date) {
    return (date / 1000).ceil();
  }

  static int _toMinutes(num date) {
    return (_toSeconds(date) / 60).ceil();
  }

  static int _toHours(num date) {
    return (_toMinutes(date) / 60).ceil();
  }

  static int _toDays(num date) {
    return (_toHours(date) / 24).ceil();
  }
}
